package com.studyadmin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.studyadmin.config.QueryPageParam;
import com.studyadmin.entity.Student;
import com.studyadmin.entity.Teacher;
import com.studyadmin.service.StudentService;
import com.studyadmin.service.TeacherService;
import com.studyadmin.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 教师表 前端控制器
 * </p>
 *
 * @author lyl
 * @since 2023-03-15
 */
@RestController
@RequestMapping("/studyadmin/teacher")
@CrossOrigin
public class TeacherController {

@Autowired
    TeacherService teacherService;

@Autowired
    StudentService studentService;
    /**
     *7772841DC6099402
     * D:\java\Tomcat\apache-tomcat-8.5.78\webapps\photos\a1.png
     * 查询学生表所有信息
     */
    @RequestMapping("/teaList")
    public JsonUtil stuList(){
        List<Teacher> list=teacherService.list();
        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setCode(0);
        jsonUtil.setData(list);
        return jsonUtil;
    }

    /**
     * 学生数据的分页查询
     * @param query
     * pageParam:{
     *         pageSize:10,			//每页显示几条
     *         pageNum:1,				//当前页数
     *         param:{					//查询条件
     *           stuName:'',			//学生姓名
     *           className:'',		//班级名称
     *           teaNum:''  //登录者教师学号
     *         }
     *       },
     * @return json.suc
     */
    @PostMapping("/listInfoPageC")
    public JsonUtil listInfoPageC(@RequestBody QueryPageParam query) {
        System.out.println(query);
        HashMap param = query.getParam();
        String stuName = (String) param.get("stuName");               //学生名称
        String className = (String) param.get("className"); //班级名称
        String teaNum = (String) param.get("teaNum"); //登录者教师学号

        Page<Student> studentPage = new Page<>();
        studentPage.setCurrent(query.getPageNum());    //设置每页多少条记录
        studentPage.setSize(query.getPageSize());      //设置当前显示第几页
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(" t.tea_num", teaNum);
        if (StringUtils.isNotBlank(stuName) && !"null".equals(stuName)){
            queryWrapper.like(" s.stu_name", stuName);
        }
        if (StringUtils.isNotBlank(className) && !"null".equals(className)){
            queryWrapper.like(" cs.cla_name", className);
        }
        IPage result = studentService.pageForStu(studentPage, queryWrapper); //最后进行分页查询


        return JsonUtil.suc(result.getRecords(), result.getTotal());
    }

    /**
     * 学生申请
     * @param teaNum
     * @return
     */
    @GetMapping("/stuApply")
    public JsonUtil stuApplyCto(@RequestParam(value = "teaNum") String teaNum){
        return teacherService.stuApplySer(teaNum);
    }

    /**
     * 同意学生申请
     * @param
     * @return
     */
    @GetMapping("/agreeStuApply")
    public JsonUtil agreeStuApplyCto(@RequestParam(value = "stuNum") String stuNum){
        return teacherService.agreeStuApplySer(stuNum);
    }
    /**
     * 同意学生申请
     * @param
     * @return
     */
    @GetMapping("/disagreeStuApply")
    public JsonUtil disagreeStuApplyCto(@RequestParam(value = "stuNum") String stuNum){
        return teacherService.disagreeStuApplySer(stuNum);
    }


    /**
     * 修改个人信息的图片
     * @param  avatar 图片地址
     *                stuNum
     * @return 结果
     */
    @GetMapping("/updateImg")
    public JsonUtil updateStuImgUrl(@RequestParam(name = "avatar") String  avatar,
                                    @RequestParam(name = "teaNum") String  teaNum) {

        Teacher teacher = new Teacher();
        teacher.setAvatar(avatar);
        teacher.setTeaNum(teaNum);
        return teacherService.updateTeaImgUrlSer(teacher);
    }

    /**
     * 教师主页信息
     * @param teaNum
     * @return
     */
    @GetMapping("/teaMain")
    public JsonUtil stuMain(@RequestParam(value = "teaNum") String teaNum){
        return teacherService.teaMainSer(teaNum);
    }
    /**
     * 修改个人信息
     * @param teacher
     * @return
     */
    @RequestMapping("/updateInfo")
    public JsonUtil updateInfo(@RequestBody Teacher teacher) {

        return teacherService.updateTeadentById(teacher);
    }







}

